home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / PolyMazesDemo-c / ColorMaze.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-04  |  8.4 KB  |  327 lines  |  [TEXT/MMCC]

  1. //•  ---------- "Source to 3D maze display" ---------- •//
  2.  
  3. //• B/W version written Jul 16, 1985 by Steve Hawley (sdh@joevax.UUCP)
  4. //• This is the source code for the maze display program. 
  5. //• It was done in Aztec C version 1.06D. 
  6.  
  7. //• Colorization by Kenneth A. Long, at "itty bitty bytes(tm)"
  8. //• Made to run in Think C and Metrowerks C.
  9.  
  10.  
  11. #include <stdio.h>
  12. #include <GestaltEqu.h>
  13.  
  14. #define kSleep                0L
  15.  
  16. #define mDevice                131
  17.  
  18. #define kNULLFilterProc        NULL
  19. Boolean        gDone;
  20.  
  21. WindowRecord    wRecord;
  22. WindowPtr    myWindow;
  23. EventRecord myEvent;
  24.  
  25. //• the maze. 1's = blocks, 0's = space.
  26. static char maze [22] [17] = { 
  27.     {1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1}, 
  28.     {1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1}, 
  29.     {1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1}, 
  30.     {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1}, 
  31.     {1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1}, 
  32.     {1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  33.     {1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1}, 
  34.     {1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1}, 
  35.     {1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1}, 
  36.     {1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1}, 
  37.     {1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1}, 
  38.     {1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1}, 
  39.     {1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1}, 
  40.     {1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1}, 
  41.     {1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1}, 
  42.     {1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1}, 
  43.     {1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1}, 
  44.     {1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 
  45.     {1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1}, 
  46.     {1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1}, 
  47.     {1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1}, 
  48.     {1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
  49. };        
  50.  
  51. //• Prototypes.
  52.  
  53. SetForeColor (short red, short green,short  blue);
  54. void        Maze_Window_Set(GDHandle device);
  55. Boolean        HasColorQD(void);
  56. GDHandle    How_Deep(void);
  57. short        Get_The_Depth(GDHandle device);
  58. void        Scramble_Color(RGBColor *colorPtr);
  59. int            Square_3_D(int column, int row);
  60. int            Draw_Maze(void);
  61. void        main(void);
  62.  
  63. //• Set the RGB forecolor with a single line.
  64. SetForeColor (short red, short green,short  blue)
  65. {
  66.     RGBColor theColor;
  67.  
  68.     theColor.red = red;
  69.     theColor.green = green;
  70.     theColor.blue = blue;
  71.     RGBForeColor(&theColor);
  72. }
  73.  
  74. //• Set the RGB backcolor with a single line.
  75. SetBackColor (short red, short green,short  blue)
  76. {
  77.     RGBColor theColor;
  78.  
  79.     theColor.red = red;
  80.     theColor.green = green;
  81.     theColor.blue = blue;
  82.     RGBBackColor(&theColor);
  83. }
  84.  
  85. void Maze_Window_Set (GDHandle device)
  86. {
  87.     WindowPtr    amazing;
  88.     Rect        globalRect, localRect;
  89.     
  90.     //• Set up the window.  Give it some bounds.
  91.     SetRect (&globalRect, 4, 40, 508, 370);
  92.     
  93.     amazing = NewCWindow (0L, &globalRect, "\pSomething Amazing", true, 0,
  94.                              (WindowPtr)-1L, false, 60L);
  95.     ShowWindow (amazing);
  96.     SetPort (amazing);
  97.  
  98.     //• Make a draw rect.  Since we set the port th the window, 
  99.     //• topLeft is now 0, 0 of the window (local).
  100.     //• This rect has the sole purpose of installing a background color.
  101.     //• Since we die on mouseDown, this is only done here.  No need to
  102.     //• insert preservation or update provisions.
  103.     SetRect (&localRect, 0, 0, 504, 330);
  104.     
  105.     //• So we set a back color.  Mac default is already set at white.
  106.     SetBackColor (0xdddd, 0xffff, 0xffff);
  107.     
  108.     //• Since it's white, we must erase it to reveal our RGBBackColor.
  109.     EraseRect (&localRect);
  110. }
  111.  
  112. Boolean    HasColorQD (void)
  113. {
  114.     unsigned char    version[ 4 ];
  115.     OSErr            err;
  116.     
  117.     err = Gestalt (gestaltQuickdrawVersion, (long *)version);
  118.     
  119.     if (err != noErr)
  120.     {
  121.         SysBeep (10);    //• Error.
  122.         ExitToShell ();
  123.     }
  124.     
  125.     if (version[ 2 ] > 0)
  126.         return (true);
  127.     else
  128.         return (false);
  129. }
  130.  
  131. GDHandle How_Deep (void)
  132. {
  133.     GDHandle    curDevice, maxDevice = NULL;
  134.     short        curDepth, maxDepth = 0;
  135.     
  136.     curDevice = GetDeviceList ();
  137.     
  138.     while (curDevice != NULL)
  139.     {
  140.         curDepth = Get_The_Depth (curDevice);
  141.         
  142.         if (curDepth > maxDepth)
  143.         {
  144.             maxDepth = curDepth;
  145.             maxDevice = curDevice;
  146.         }
  147.  
  148.         curDevice = GetNextDevice (curDevice);
  149.     }
  150.     
  151.     return (maxDevice);
  152. }
  153.  
  154. short Get_The_Depth (GDHandle device)
  155. {
  156.     PixMapHandle    screenPixMapH;
  157.     
  158.     screenPixMapH = (**device).gdPMap;
  159.     
  160.     return ((**screenPixMapH).pixelSize);
  161. }
  162.  
  163. //• This scramble will give a random RGB for each poly that calls it.
  164. //• I set specifics instead, but left this here for data purposes.
  165. void Scramble_Color (RGBColor *colorPtr)
  166. {
  167.     colorPtr->red =  Random () + 32767;
  168.     colorPtr->blue = Random () + 32767;
  169.     colorPtr->green = Random () + 32767;
  170. }
  171.  
  172. //• Displays a given block from the maze. Decides on which side of 
  173. //• center the block is found, and draws the visible faces, which 
  174. //• are defined as polygons. The faces are projected using the
  175. //• formulae: 
  176. //•           'x' = x / z; 
  177. //•           'y' = y / z;         
  178. //• to achieve one point perspective.
  179.  
  180. Square_3_D (int column, int row)
  181. {
  182.     RGBColor color, myColor;
  183.     int x1, x2, y1, y2, z1, z2;
  184.     PolyHandle left_side, right_side, top_side, front_side;
  185.     
  186.     x1 = (column - 8) * 100;        //• 100 is width of side.
  187.     x2 = x1 + 100;
  188.     z1 = (23 - row);
  189.     z2 = z1 -1;
  190.     y1 = 150;
  191.     y2 = 250;
  192.     
  193.     left_side = OpenPoly ();
  194.     MoveTo ((x1 / z1) + 256, (y1 / z1) + 60);
  195.     LineTo ((x1 / z1) + 256, (y2 / z1) + 60);
  196.     LineTo ((x1 / z2) + 256, (y2 / z2) + 60);
  197.     LineTo ((x1 / z2) + 256, (y1 / z2) + 60);
  198.     LineTo ((x1 / z1) + 256, (y1 / z1) + 60);
  199.     ClosePoly ();
  200.     
  201.     right_side = OpenPoly ();
  202.     MoveTo ((x2 / z1) + 256, (y1 / z1) + 60);
  203.     LineTo ((x2 / z1) + 256, (y2 / z1) + 60);
  204.     LineTo ((x2 / z2) + 256, (y2 / z2) + 60);
  205.     LineTo ((x2 / z2) + 256, (y1 / z2) + 60);
  206.     LineTo ((x2 / z1) + 256, (y1 / z1) + 60);
  207.     ClosePoly ();
  208.     
  209.     top_side = OpenPoly ();
  210.     MoveTo ((x1 / z1) + 256, (y1 / z1) + 60);
  211.     LineTo ((x2 / z1) + 256, (y1 / z1) + 60);
  212.     LineTo ((x2 / z2) + 256, (y1 / z2) + 60);
  213.     LineTo ((x1 / z2) + 256, (y1 / z2) + 60);
  214.     LineTo ((x1 / z1) + 256, (y1 / z1) + 60);
  215.     ClosePoly ();
  216.     
  217.     front_side = OpenPoly ();
  218.     MoveTo ((x1 / z2) + 256, (y1 / z2) + 60);
  219.     LineTo ((x2 / z2) + 256, (y1 / z2) + 60);
  220.     LineTo ((x2 / z2) + 256, (y2 / z2) + 60);
  221.     LineTo ((x1 / z2) + 256, (y2 / z2) + 60);
  222.     LineTo ((x1 / z2) + 256, (y1 / z2) + 60);
  223.     ClosePoly ();
  224.     
  225.     //• Decide to draw right side (dark gray).
  226.     if (x2 < 0 && maze [row] [column + 1] != 1)
  227.     {
  228. //        Scramble_Color (&color);
  229. //        RGBForeColor (&color);
  230.  
  231.         //• Uncomment the above two lines 
  232.         //• and comment out the below line,
  233.         //• and do the same for similar sets below for UGLY random.
  234.  
  235.         //• 4444 is 5/16 of the range.
  236.         SetForeColor (0x4444, 0x4444, 0x4444);
  237.         ErasePoly (right_side);
  238.         PaintPoly (right_side);
  239.         SetForeColor (0, 0, 0);        //• Our line is "off" or black.
  240.         FramePoly (right_side);
  241.     }
  242.     
  243.     //• Decide to draw left face (light gray).
  244.     if (x1 > 0 && maze [row] [column - 1] != 1) 
  245.     {
  246. //        Scramble_Color (&color);
  247. //        RGBForeColor (&color);
  248.         SetForeColor (0xbbbb, 0xbbbb, 0xbbbb);    //• 11/16 toward full on.
  249.         ErasePoly (left_side);
  250.         PaintPoly (left_side);
  251.         
  252.         SetForeColor (0, 0, 0);
  253.         FramePoly (left_side);
  254.     }
  255.  
  256.     //• Draw the top_side (white).
  257. //    Scramble_Color (&color);
  258. //    RGBForeColor (&color);
  259.     SetForeColor (0xeeee, 0xeeee, 0xeeee);    //• 15/16 full on.
  260.     ErasePoly (top_side);
  261.     PaintPoly (top_side);
  262.     SetForeColor (0, 0, 0);
  263.     FramePoly (top_side);
  264.     
  265.     //• Draw the front_side (gray).
  266. //    Scramble_Color (&color);
  267. //    RGBForeColor (&color);
  268.     SetForeColor (0x7777, 0x7777, 0x7777);    //• 7/16 full on.
  269.     ErasePoly (front_side);
  270.     PaintPoly (front_side);
  271.     
  272.     SetForeColor (0, 0, 0);
  273.     FramePoly (front_side);
  274.     
  275.     //• Restore pen characteristics.
  276.     PenNormal (); 
  277.     
  278.     //• Dispose of all the polygons to free up memory.
  279.     KillPoly (top_side);        
  280.     KillPoly (front_side);
  281.     KillPoly (left_side);
  282.     KillPoly (right_side);
  283. }
  284.  
  285. //• Draws out all the blocks, starting from the back, moving left 
  286. //• to center, then right to center.
  287.  
  288. Draw_Maze ()
  289. {
  290.     Rect trect;
  291.     int row, column;
  292.     
  293.     for (row = 0; row < 22; row++)
  294.     {
  295.         for (column = 0; column < 8; column++)
  296.             if (maze [row] [column] == 1) 
  297.                 Square_3_D (column, row);
  298.         for (column = 16; column > 7; column--)
  299.             if (maze [row] [column] == 1) 
  300.                 Square_3_D (column, row);
  301.     }
  302. }
  303.  
  304. void    main (void)
  305. {
  306.     InitGraf (&qd.thePort);
  307.     InitFonts ();
  308.     InitWindows ();
  309.     InitMenus ();
  310.     TEInit ();
  311.     InitDialogs (nil);
  312.     InitCursor ();
  313.  
  314.     //• No color?  Then you don't get to play!
  315.     if (! HasColorQD ())
  316.         ExitToShell ();
  317.             
  318.     //• HowDeep is not really needed, since all our colors are set
  319.     //• at divisions of 16.  But in case you need it, it was left in.
  320.     Maze_Window_Set (How_Deep ());    
  321.     
  322.     Draw_Maze ();
  323.     
  324.     while (!Button ())     //• Click and exit.
  325.         GetNextEvent (everyEvent, &myEvent);
  326. }
  327.